import 'package:flutter/material.dart';

import 'package:flutter_demo/pages/homePage.dart';
import 'package:flutter_demo/pages/categoryPage.dart';
import 'package:flutter_demo/pages/carPage.dart';
import 'package:flutter_demo/pages/mePage.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Shop',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  int _currentIndex = 0;
  String _currentTitle = "首页";
  var _pageController = new PageController(initialPage: 0);
  List<String> _tabBars = [
    "WOW",
    "精品推荐",
    "夏季穿搭",
    "新奇文创",
    "送给他",
    "送给她",
    "夏日大作战",
    "食品饮料",
    "个护健康",
    "箱包配饰",
    "手机数码",
  ];
  List<BottomBarItem> bottomBarItems = [
    new BottomBarItem(
      icon: "images/home.png",
      iconUN: "images/home_un.png",
      title: "首页",
    ),
    new BottomBarItem(
      icon: "images/category.png",
      iconUN: "images/category_un.png",
      title: "分类",
    ),
    new BottomBarItem(
      icon: "images/car.png",
      iconUN: "images/car_un.png",
      title: "购物车",
    ),
    new BottomBarItem(
      icon: "images/me.png",
      iconUN: "images/me_un.png",
      title: "我的",
    ),
  ];
  List<Widget> _pages = [
    new HomePage(),
    new CategoryPage(),
    new CarPage(),
    new MePage(),
  ];

  void _pageChange(int index) {
    setState(() {
      if (_currentIndex != index) {
        _currentIndex = index;
        _currentTitle = bottomBarItems[index].title;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(_currentTitle),
        elevation: 0.0,
        centerTitle: true,
        bottom: _currentIndex == 0
            ? new TabBar(
                controller:
                    new TabController(length: _tabBars.length, vsync: this),
                isScrollable: true,
                tabs: _tabBars.map(
                  (String item) {
                    return new Tab(
                      text: item,
                    );
                  },
                ).toList())
            : null,
      ),
      body: new PageView.builder(
        onPageChanged: _pageChange,
        controller: _pageController,
        itemCount: _pages.length,
        itemBuilder: (BuildContext context, int index) {
          return _pages[index];
        },
      ),
      bottomNavigationBar: new BottomNavigationBar(
        items: bottomBarItems.map((BottomBarItem item) {
          return BottomNavigationBarItem(
            icon: new Image.asset(
              item.icon,
              width: 24.0,
              height: 24.0,
            ),
            title: new Text(item.title),
          );
        }).toList(),
        currentIndex: _currentIndex,
        onTap: (int index) {
          _pageController.animateToPage(
            index,
            duration: Duration(seconds: 2),
            curve: ElasticOutCurve(0.8),
          );
          _pageChange(index);
        },
        type: BottomNavigationBarType.fixed,
      ),
    );
  }
}

class BottomBarItem {
  final String icon;
  final String iconUN;
  final String title;
  BottomBarItem({this.icon, this.iconUN, this.title});
}
